package com.mtw.bbs.common.core.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.mtw.bbs.common.core.enums.ResultCode;
import com.mtw.bbs.common.core.exception.BaseException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.io.Serializable;
import java.time.Instant;
import java.time.ZonedDateTime;

import static com.mtw.bbs.common.core.enums.ResultCode.SERVICE_EXP;


@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
@ApiModel(description = "rest请求的返回模型，所有rest正常都返回该类的对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    public static final String SUCCESSFUL_CODE = ResultCode.SUCCESS.getCode();
    public static final String SUCCESSFUL_MESG = ResultCode.SUCCESS.getMsg();

    @ApiModelProperty(value = "处理结果code", required = true)
    private String code;

    @ApiModelProperty(value = "处理结果描述信息")
    private String mesg;

    @ApiModelProperty(value = "请求结果生成时间戳")
    private Instant time;

    @ApiModelProperty(value = "处理结果数据信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Result() {
        this.time = ZonedDateTime.now().toInstant();
    }

    

    /**
     * @param data
     */
    public Result(T data) {
        this();
        this.data = data;
    }

    /**
     * 内部使用，用于构造成功的结果
     */
    private Result(String code, String mesg, T data) {
        this.code = code;
        this.mesg = mesg;
        this.data = data;
        this.time = ZonedDateTime.now().toInstant();
    }



    /**
     * 快速创建成功结果
     */
    public static Result success() {
        return success(null);
    }


    /**
     * 快速创建成功结果并返回结果数据
     */
    public static Result success(Object data) {
        return new Result<>(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data);
    }


    /**
     * 快速创建成功结果并返回结果数据
     */
    public static Result success(String code,Object data) {
        return new Result<>(code, SUCCESSFUL_MESG, data);
    }


    /**
     * 快速创建成功结果并返回结果数据
     */
    public static Result success(String code,String msg,Object data) {
        return new Result<>(code, msg, data);
    }






    /**
     * 快速创建失败结果
     */
    public static Result fail(String msg) {
        return Result.fail(SERVICE_EXP.getCode(), msg);
    }


    /**
     * 系统异常类没有返回数据
     */
    public static Result fail(ResultCode resultCode) {
        return new Result<>(resultCode.getCode(), resultCode.getMsg(), null);
    }


    /**
     * 系统异常类没有返回数据
     */
    public static Result fail(BaseException baseException) {
        return new Result<>(baseException.getErrorCode(),baseException.getMessage(), null);
    }



    /**
     * 系统异常类并返回结果数据
     */
    public static Result fail(String errorCode, String msg) {
        return new Result<>(errorCode, msg,null);
    }






    /**
     * 成功code=000000
     */
    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESSFUL_CODE.equals(this.code);
    }

    /**
     * 失败
     */
    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }
}
